﻿using System;
using Microsoft.FxCop.Sdk;

namespace Community.StaticAnalysis.Design.Rules
{
    /// <summary>
    /// The <see cref="ScopeDataContractClassAsPublic"/>
    /// class is used to identify classes marked with the DataContract attribute that are not public.
    /// </summary>
    internal sealed class ScopeDataContractClassAsPublic : BaseIntrospectionRule
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ScopeDataContractClassAsPublic"/> class.
        /// </summary>
        public ScopeDataContractClassAsPublic()
            : base(
                typeof (ScopeDataContractClassAsPublic).Name,
                typeof (ScopeDataContractClassAsPublic).Namespace + Constants.RulesResourceNameSuffix,
                typeof (ScopeDataContractClassAsPublic).Assembly)
        {
        }

        /// <summary>
        /// Checks the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public override ProblemCollection Check(TypeNode type)
        {
            // Check if this class is marked with DataContract
            if (RulesHelper.NodeHasAttribute(type, Constants.DataContractFullName) == false)
            {
                // This is a class that isn't marked with DataContract attribute
                return null;
            }

            // Check if the class is public
            if (type.IsPublic == false)
            {
                // This data contract class is not public
                Problems.Add(new Problem(GetResolution(new Object[] {type.FullName})));
            }

            return Problems;
        }
    }
}